﻿using System;
using System.Collections.Generic;

namespace Wicresoft.Core.Mvc.Pager
{
    /// <summary>
    ///     <para>2010/8/13</para>
    ///     <para>TANGJINGBO</para>
    ///     <para>tangjingbo</para>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class PagedList<T> : List<T> //, IPagedList<T>
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="PagedList&lt;T&gt;" /> class.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        public PagedList(IList<T> items, int pageIndex, int pageSize)
        {
            PageSize = pageSize;
            TotalItemCount = items.Count;
            TotalPageCount = (int) Math.Ceiling(TotalItemCount/(double) PageSize);
            CurrentPageIndex = pageIndex;
            StartRecordIndex = (CurrentPageIndex - 1)*PageSize + 1;
            EndRecordIndex = TotalItemCount > pageIndex*pageSize ? pageIndex*pageSize : TotalItemCount;
            for (int i = StartRecordIndex - 1; i < EndRecordIndex; i++)
            {
                Add(items[i]);
            }
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="PagedList&lt;T&gt;" /> class.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalItemCount">The total item count.</param>
        public PagedList(IEnumerable<T> items, int pageIndex, int pageSize, int totalItemCount)
        {
            AddRange(items);
            TotalItemCount = totalItemCount;
            TotalPageCount = (int) Math.Ceiling(totalItemCount/(double) pageSize);
            CurrentPageIndex = pageIndex;
            PageSize = pageSize;
            StartRecordIndex = (pageIndex - 1)*pageSize + 1;
            EndRecordIndex = TotalItemCount > pageIndex*pageSize ? pageIndex*pageSize : totalItemCount;
        }

        /// <summary>
        ///     当前页索引 1起始
        /// </summary>
        /// <value>The index of the current page.</value>
        public int CurrentPageIndex { get; set; }

        /// <summary>
        ///     Gets or sets the size of the page.
        /// </summary>
        /// <value>The size of the page.</value>
        public int PageSize { get; set; }

        /// <summary>
        ///     Gets or sets the total item count.
        /// </summary>
        /// <value>The total item count.</value>
        public int TotalItemCount { get; set; }

        /// <summary>
        ///     Gets or sets the total page count.
        /// </summary>
        /// <value>The total page count.</value>
        public int TotalPageCount { get; private set; }

        /// <summary>
        ///     Gets or sets the start index of the record.
        /// </summary>
        /// <value>The start index of the record.</value>
        public int StartRecordIndex { get; private set; }

        /// <summary>
        ///     Gets or sets the end index of the record.
        /// </summary>
        /// <value>The end index of the record.</value>
        public int EndRecordIndex { get; private set; }


        /// <summary>
        ///     获取一个值，指示当前页码是否无效而需重定向
        /// </summary>
        /// <value>
        ///     <c>true</c> if [need redirect]; otherwise, <c>false</c>.
        /// </value>
        public bool NeedRedirect
        {
            get { return (Count == 0 && TotalPageCount < CurrentPageIndex); }
        }
    }
}